//
//  GetListViewController.m
//  HGMMT
//
//  Created by eport_user1 on 2018/4/16.
//  Copyright © 2018年 eport_user1. All rights reserved.
//

#import "GetListViewController.h"
#import "LinkServers.h"
static NSString *iden = @"inden_cell";

@interface GetListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *listMutaArray;

@end

@implementation GetListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self _initNav];
    [self _initData];
    [self _initView];
    
}

-(void)_initNav{
    self.title = @"选择IDE列表连接";
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"清空列表" style:(UIBarButtonItemStylePlain) target:self action:@selector(clearButtonAction:)];
    self.navigationItem.rightBarButtonItem = rightItem;
    
}

-(void)clearButtonAction:(UIButton *)button{
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:nil forKey:SQLITEDEFEATS];
    [defaults synchronize];
    
    [self.listMutaArray removeAllObjects];
    [self.tableView reloadData];
    
}



-(void)_initView{
    
    self.tableView = [[UITableView alloc]initWithFrame:kScreenBounds style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [Tool setExtraCellLineHidden:self.tableView];
    [self.view addSubview:self.tableView];
}


#pragma mark -代理协议

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.listMutaArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:iden];
    }
    
    cell.textLabel.text = self.listMutaArray[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

-(void)_initData{
    
    NSArray *array = [[NSUserDefaults standardUserDefaults]objectForKey:SQLITEDEFEATS];
    
    self.listMutaArray = [NSMutableArray arrayWithArray:array];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 60;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 40;
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
    view1.backgroundColor = [UIColor lightGrayColor];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15, (40 - 20)/2.0,(kScreenWidth - 15* 2), 20)];
    label.font = [UIFont systemFontOfSize:15];
    label.text = @"点击IDE列表与开发套件进行连接";
    [view1 addSubview:label];
    return view1;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSString *code = self.listMutaArray[indexPath.row];
    
    [[LinkServers shareInstance] linkWithIdeCode:code];
    
    [LinkServers shareInstance].linkBlock = ^(BOOL isSuccess) {
        
        if (isSuccess == NO) {
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"连接失败" message:@"请检查测试门户与开发套件是否处于同一网络下" preferredStyle:(UIAlertControllerStyleAlert)];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                
                [self.navigationController popViewControllerAnimated:YES];
                
            }]];
            
            [self presentViewController:alert animated:YES completion:nil];
        }
        
    };

}

@end
